package com.richard.cloud;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.richard.cloud.config.CustomerBlockHandler;
import com.richard.cloud.entity.Payment;

@SpringBootApplication
@EnableDiscoveryClient
public class BootProviderPaymenet9001 {
	public static void main(String[] args) {
		SpringApplication.run(BootProviderPaymenet9001.class, args);
	}
	
	@RestController
    public class TestController {
		@Value("${server.port}")
		int port = 9001;
		
        @GetMapping(value = "/hello")
        public String hello() {
            return "Hello @ " + LocalDateTime.now();
        }
        
        @GetMapping("/payment/{id}")
        public Payment payment(@PathVariable Long id) {
        	Payment payment = new Payment();
        	payment.setId(id);
        	payment.setPort(port);
            return payment;
        }
        
        @GetMapping(value = "/sentinel/test1")
        public String test_sentinel1() {
            return "Hello @ " + LocalDateTime.now();
        }
        @GetMapping(value = "/sentinel/test2")
        public String test_sentinel2() {
            return "Hello @ " + LocalDateTime.now();
        } 
        
        //窗口期一过，则重新开始降级/限流阈值统计，下面就会报 RuntimeException
        @GetMapping(value = "/sentinel/exrate")
        public String test_sentinel_ex() {
        	int i = 10/0;
            return "Hello @ " + LocalDateTime.now();
        }
        
        @SentinelResource(value = "testHotkey")
        @GetMapping("/sentinel/hotkey")
        public String test_hot_key(@RequestParam(required = false) String p1
        						 , @RequestParam(required = false) String p2) {
        	return p1 + p2;
        }
        public String handle_test_hot_key(String p1, String p2, BlockException ex) {
        	return "Param limit flow";
		}
        
        @SentinelResource(value = "testBlockHandler"
        				, blockHandlerClass = CustomerBlockHandler.class, blockHandler ="handlerException")
        @GetMapping("/sentinel/testBlockHandler")
        public String testBlockHandler() {
        	return "no flow limit!";
        }
        
        @GetMapping("/sentinel/testFallBack")
        @SentinelResource(value = "fallBack", fallback = "fallback")
        public String testFallBack() {
        	return "" + 1L/0;
        }
        public String fallback() {
        	return "fallback!";
        }
    }
}
